﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Biblio2.GUI;
using Biblio2.Models;

namespace Biblio2.LocalParsing
{
    public class LocalParsingManager
    {
        #region Attributs
        private FileLister m_filelister;
        private FileNameParser m_fileNameParser;
        public enum Step
        {
            Step1_1,
            Step1_2,
            Step1_3,
            Step1_4,
        }
	    #endregion

        #region Singleton
        private static LocalParsingManager m_instance = null;

        private LocalParsingManager() 
        {
            InitFileLister();
            InitFileNameParser();
        }

        public static LocalParsingManager GetInstance()
        {
            if (m_instance == null)
                m_instance = new LocalParsingManager();

            return m_instance;
        }
        #endregion Singleton

        #region Public
        public static void StartLocalParsing()
        {
            GetInstance().StartFileListerParsing();
        }

        public static void CancelLocalParsing()
        {
            GetInstance().CancelAllParsing();
        }
        #endregion Public

        #region Privé
        private void CancelAllParsing()
        {
            CancelFileListerParsing();
            CancelFileNameParsing();
            GUIMain.LocalParsingUC.SetStep(LocalParsingUserControl.Step.None);
        }

        private void ReportProgress(object sender, ProgressChangedEventArgs e)
        {
            WorkerEventProgress eventProgress = e.UserState as WorkerEventProgress;

            if (eventProgress.Type == WorkerEventProgress.ReportEnum.Progress)
            {
                GUIMain.LocalParsingUC.Step1UC.ReportProgress(eventProgress.Step, eventProgress.ProgressValue, eventProgress.ProgressMaxValue);
                GUIMain.LocalParsingUC.Step1UC.ReportProgress(eventProgress.Step, eventProgress.Message);
            }
            else if (eventProgress.Type == WorkerEventProgress.ReportEnum.Error)
            {
                GUIMain.LocalParsingUC.Step1UC.ReportError(eventProgress.Step, eventProgress.Message);
            }
        }
        #endregion Privé

        #region FileLister
        private void InitFileLister()
        {
            m_filelister = new FileLister();
            m_filelister.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(FileListerRunWorkerCompleted);
            m_filelister.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(ReportProgress);
        }

        private void StartFileListerParsing()
        {
            if ((!m_filelister.IsBusy) && (!m_filelister.CancellationPending))
            {
                GUIMain.LocalParsingUC.SetStep(LocalParsingUserControl.Step.Step1);
                m_filelister.RunWorkerAsync();
            }
        }

        private void FileListerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                
            }
            else
            {
                List<PathPair> list = e.Result as List<PathPair>;
                StartFileNameParsing(list);
            }
        }
        
        public void CancelFileListerParsing()
        {
            if ((m_filelister.IsBusy) && (!m_filelister.CancellationPending))
                m_filelister.CancelAsync();
        }
        #endregion FileLister

        #region FileNameParser
        private void InitFileNameParser()
        {
            m_fileNameParser = new FileNameParser();
            m_fileNameParser.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(FileNameParserRunWorkerCompleted);
            m_fileNameParser.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(ReportProgress);
        }

        private void StartFileNameParsing(List<PathPair> aList)
        {
            if ((!m_fileNameParser.IsBusy) && (!m_fileNameParser.CancellationPending))
                m_fileNameParser.RunWorkerAsync(aList);
        }

        private void FileNameParserRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                
            }
            else
            {
                List<LocalParseResult> list = e.Result as List<LocalParseResult>;
                GUIMain.LocalParsingUC.FileListEditUC.StartFilesInfosEdition(list);
            }
        }
        
        public void CancelFileNameParsing()
        {
            if ((m_fileNameParser.IsBusy) && (!m_fileNameParser.CancellationPending))
                m_fileNameParser.CancelAsync();
        }
        #endregion FileLister
    }
}

